package com.liujit.bms.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liujit.bms.admin.domain.model.BlogArticle;
import com.liujit.bms.admin.domain.model.BlogArticleLabel;
import com.liujit.bms.admin.domain.params.BlogArticleLabelParams;
import com.liujit.bms.admin.domain.params.BlogArticleParams;
import com.liujit.bms.admin.domain.query.BlogArticleQuery;
import com.liujit.bms.admin.domain.result.BlogArticleResult;
import com.liujit.bms.admin.mapper.BlogArticleMapper;
import com.liujit.bms.admin.service.BlogArticleLabelService;
import com.liujit.bms.admin.service.BlogArticleService;
import com.liujit.bms.admin.service.BlogCategoryService;
import com.liujit.common.constants.ColumnConstant;
import com.liujit.common.constants.GlobalConstant;
import com.liujit.common.domain.params.BaseParams;
import com.liujit.common.domain.result.PageBean;
import com.liujit.common.exception.DataNotFoundException;
import com.liujit.common.utils.DozerConvertor;
import com.liujit.common.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description 博客文章Service实现类
 * @Author liujun
 * @Date 2021-07-03 14:54:49
 */
@Service
public class BlogArticleServiceImpl extends ServiceImpl<BlogArticleMapper, BlogArticle> implements BlogArticleService {

    private BlogArticleLabelService blogArticleLabelService;

    private BlogCategoryService blogCategoryService;

    @Autowired
    public void setBlogArticleLabelService(BlogArticleLabelService blogArticleLabelService) {
        this.blogArticleLabelService = blogArticleLabelService;
    }

    @Autowired
    public void setBlogCategoryService(BlogCategoryService blogCategoryService) {
        this.blogCategoryService = blogCategoryService;
    }

    @Override
    public PageBean<BlogArticleResult> getPage(BlogArticleQuery query) {
        // 排序
        if (StrUtil.isNotBlank(query.getSortName()) && StrUtil.isNotBlank(query.getSortOrder())) {
            query.setSortName(ColumnConstant.CREATE_TIME);
            query.setSortOrder(SqlKeyword.DESC.getSqlSegment());
        }
        Page<BlogArticle> page = Pagination.page(query);
        LambdaQueryWrapper<BlogArticle> wrapper = new LambdaQueryWrapper();
        if (query.getCategoryId() != null) {
            wrapper.eq(BlogArticle::getCategoryId, query.getCategoryId());
        }
        if (StrUtil.isNotBlank(query.getAuthor())) {
            wrapper.like(BlogArticle::getAuthor, query.getAuthor());
        }
        if (StrUtil.isNotBlank(query.getTitle())) {
            wrapper.like(BlogArticle::getTitle, query.getTitle());
        }
        if (query.getIsOpen() != null) {
            wrapper.eq(BlogArticle::getIsOpen, query.getIsOpen());
        }
        if (query.getIsTop() != null) {
            wrapper.eq(BlogArticle::getIsTop, query.getIsTop());
        }
        if (query.getStartTime() != null) {
            wrapper.ge(BlogArticle::getCreateTime, query.getStartTime());
        }
        if (query.getEndTime() != null) {
            wrapper.le(BlogArticle::getCreateTime, query.getEndTime());
        }
        IPage<BlogArticle> iPage = super.page(page, wrapper);
        return PageBean.page(iPage, BlogArticleResult.class);
    }

    @Override
    public Boolean save(BlogArticleParams params) {
        BlogArticle entity = DozerConvertor.convertor(params, BlogArticle.class);
        // 添加
        if (params.getId() == null) {
            entity.setReadNum(0);
            entity.setCommentNum(0);
            entity.setLikeNum(0);
            Boolean result = super.save(entity);
            blogArticleLabelService.save(new BlogArticleLabelParams() {{
                setArticleId(entity.getId());
                setLabelIds(params.getLabelIds());
            }});
            return result;
        } else {
            BlogArticle blogArticle = super.getById(params.getId());
            if (blogArticle == null) {
                throw new DataNotFoundException();
            }
            blogArticleLabelService.save(new BlogArticleLabelParams() {{
                setArticleId(params.getId());
                setLabelIds(params.getLabelIds());
            }});
            return super.updateById(entity);
        }
    }

    @Override
    public BlogArticleResult get(Long id) {
        BlogArticle blogArticle = super.getById(id);
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        BlogArticleResult result = DozerConvertor.convertor(blogArticle, BlogArticleResult.class);
        result.setLabelIds(blogArticleLabelService.getLabelIdByArticleId(id));
        return result;
    }

    @Override
    public BlogArticleResult getDetail(Long id) {
        BlogArticle blogArticle = super.getById(id);
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        BlogArticleResult result = DozerConvertor.convertor(blogArticle, BlogArticleResult.class);
        // 分类名称
        result.setCategoryName(blogCategoryService.getNameById(blogArticle.getCategoryId()));
        // 标签名称
        result.setLabelName(blogArticleLabelService.getLabelNameByArticleId(id));
        return result;
    }

    @Override
    public Boolean del(BaseParams params) {
        BlogArticle blogArticle = super.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        // 删除文章标签
        LambdaQueryWrapper<BlogArticleLabel> wrapper = new LambdaQueryWrapper();
        wrapper.eq(BlogArticleLabel::getArticleId, params.getId());
        blogArticleLabelService.remove(wrapper);
        return super.removeById(params.getId());
    }

    @Override
    public Boolean open(BaseParams params) {
        BlogArticle blogArticle = super.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogArticle() {{
            setId(params.getId());
            setIsOpen(GlobalConstant.OPEN_YES);
        }});
    }

    @Override
    public Boolean cancelOpen(BaseParams params) {
        BlogArticle blogArticle = super.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogArticle() {{
            setId(params.getId());
            setIsOpen(GlobalConstant.OPEN_NO);
        }});
    }

    @Override
    public Boolean top(BaseParams params) {
        BlogArticle blogArticle = super.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogArticle() {{
            setId(params.getId());
            setIsTop(GlobalConstant.TOP_YES);
        }});
    }

    @Override
    public Boolean cancelTop(BaseParams params) {
        BlogArticle blogArticle = super.getById(params.getId());
        if (blogArticle == null) {
            throw new DataNotFoundException();
        }
        return super.updateById(new BlogArticle() {{
            setId(params.getId());
            setIsTop(GlobalConstant.TOP_NO);
        }});
    }
}
